sugarfreeApp.factory('ProfileService',['HttpService','$filter',function (httpService,$filter) {
  var service = {
		  retrieveProfile:retrieveProfile,
		  generateProfile:generateProfile,
      checkField:checkField,
      updateProfile:updateProfile,
      removeProfileImg:removeProfileImg,
      getImageUpToken:getImageUpToken,
      getLogoUpToken:getLogoUpToken
  };
  return service;
  
  
  
  
  function retrieveProfile(url,data){
	  return httpService.get(url);
  }
  
  
  function generateProfile(profile,shengshis,coachs,deviceCheckeboxs,devices){
	  var timeOpenTokens = profile.openTime.split(':');
      profile.topenTime = new Date(1970,0,1, timeOpenTokens[0], timeOpenTokens[1]);
      var timeCloseTokens = profile.closeTime.split(':');
      profile.tcloseTime = new Date(1970,0,1, timeCloseTokens[0], timeCloseTokens[1]);
      // hour & min steps -- used to adjust open and close time on UI
      profile.hstep = 1;
      profile.mstep = 30;
      
      var found = $filter('filter')(coachs,{id:profile.contactId}, true);
      if(found && found.length){
    	  profile.contactName = found[0].name;
    	  profile.contactMobile = found[0].mobile;
      }
      // set deviceCheckeboxs checked value
      if(profile.devices && profile.devices.length > 0){
    	  for(var i = 0 ; i < deviceCheckeboxs.length ; i++){
    		  for(var j=0; j<profile.devices.length ; j++){
    			  if(deviceCheckeboxs[i].value === profile.devices[j].value){
    				  deviceCheckeboxs[i].checked = true;
    			  }
    		  }
    	  }
      }
	  return profile;
  }


  function checkField(profile,msg){
      var valid = true;
      var errMsg = {};
      if(!profile.region.id){
        errMsg.emptyRegion = msg.emptyRegion;
        valid = false;
      }else{
        errMsg.emptyRegion = "";
      }
      
      if(!profile.contactId){
        errMsg.emptyContact = msg.emptyContact;
        valid = false;
      }else{
        errMsg.emptyContact = "";
      }

      return {valid:valid,errMsg:errMsg};
  }


  function updateProfile(profile,profileEditImages){
        var openH  = $filter('date')(profile.topenTime, "HH");
        var openM  = $filter('date')(profile.topenTime, "mm");
        var closeH = $filter('date')(profile.tcloseTime, "HH");
        var closeM = $filter('date')(profile.tcloseTime, "mm");
        var openMinute  = parseInt(openH) * 60 + parseInt(openM);
        var closeMinute = parseInt(closeH)* 60 + parseInt(closeM);
        var _dataStr = "name=" + profile.name +
                       "&openTime=" + openMinute + 
                       "&closeTime=" + closeMinute +
                       "&regionId=" + profile.region.id +
                       "&address=" + profile.address +
                       "&contactId=" + profile.contactId +
                       "&description=" + profile.description +
                       "&devices=" + profile.deviceSum;
				var _imgStr = "";
//  	  console.error("profileEditImages",profileEditImages);
    	  var editImages = [];
    	  for(var i=0 ; i < profileEditImages.length ; i++){
    	  	_imgStr +="&images=" + profileEditImages[i].key;
    	  	editImages.push(profileEditImages[i].key);
    	  }
    	  var _delImgStr = "";
    	  if(profile.deletedImages){
    	  	for(var i=0 ; i < profile.deletedImages.length ; i++){
    	  		_delImgStr +="&deletedImages=" + profile.deletedImages[i];
    	  	}
    	  }
    	  _dataStr += _imgStr;
    	  
    	  _dataStr += _delImgStr;
        var updateProfileURL = "profile/update";
        
        
        /*==============================================*/
//     console.info('profile.profileEditImages',profile.profileEditImages);
       var __dataStr = {
       	 "name":profile.name,
	       "openTime":openMinute,
	       "closeTime":closeMinute,
	       "regionId":profile.region.id,
	       "address":profile.address,
	       "contactId":profile.contactId,
	       "description":profile.description,
	       "devices":profile.deviceSum,
	       "images":editImages,
	       "deletedImages":profile.deletedImages
       };
        return httpService.postJson(updateProfileURL,__dataStr);
  }

  function removeProfileImg(imgId){
      var removeImgURL = "profile/image/delete";
      var _dataStr = "ids="+ imgId;
      return httpService.post(removeImgURL,_dataStr);
  }


  function getImageUpToken(){
    var getUptoKenUrl = "profile/image/uptoken";
    return httpService.get(getUptoKenUrl);
  }

  function getLogoUpToken(){
    var getUptoKenUrl = "profile/logo/uptoken";
    return httpService.get(getUptoKenUrl);
  }
}]);